package jiequ;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class log {

	public static void main(String[] args) {
		String path = "E:\\log.log";
		File file = new File(path);
		if (file.exists() && file.isFile()) {
			FileInputStream fileInputs = null;
			try {
				fileInputs = new FileInputStream(file);
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputs);
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				String line =null;
				File ipFile = new File("E:\\iplog.txt");
					if(!ipFile.exists()){
						ipFile.createNewFile();
					}
				FileOutputStream fileOutputStream = new FileOutputStream(ipFile);
				BufferedOutputStream  bufferedOutputStream  = new BufferedOutputStream  (fileOutputStream);
				while((line=bufferedReader.readLine()) != null){
					//System.out.println(log.getIP(line));
					fileOutputStream.write(log.getIP(line).getBytes());
					fileOutputStream.write("\n".getBytes());
				}
				fileOutputStream.flush();
				fileOutputStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("not a full");
		}
		System.out.println("end");
	}
	public static String getIP(String content){
				String regUrl = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
        Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(content);
        boolean blnp = m.find();
		if(blnp==true){
			return m.group();
		}
        return null;
	}

}
